const { DataTypes } = require("sequelize");
const sequelize = require("../config/database");
const Product = require("./Product");

const Sale = sequelize.define("Sale", {
  productId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '商品ID'
  },
  quantity: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '销售数量'
  },
  price: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    comment: '销售时的单价'
  },
  total: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    comment: '销售总额'
  }
});

// 建立与商品的关联关系
Sale.belongsTo(Product, {
  foreignKey: 'productId',
  as: 'product'
});

module.exports = Sale;